package com.fadis.cmd;

// min = (5  开区间
// min = [5 闭区间，默认
// min = -inf 负无限  max = +inf 正无限
// 返回score 值在 min 和 max 之间的成员的数量（include）
// zrangebyscore set1 -inf (+inf    这种也支持
// zrangebyscore set1 +inf -inf   这种也支持，不过返回null
public class ZSetScoreRange {

    @Override
    public String toString() {
        return "ZSetScoreRange{" +
                "min=" + min +
                ", max=" + max +
                ", minInclude=" + minInclude +
                ", maxInclude=" + maxInclude +
                '}';
    }

    public double min = 0;
    public double max = 0;
    public boolean minInclude = true;
    public boolean maxInclude = true;

    public static ZSetScoreRange from(String minStr, String maxStr){
        ZSetScoreRange r = new ZSetScoreRange();

        if(minStr.equals("-inf") || minStr.equals("(-inf") || minStr.equals("[-inf")){
            r.min = Double.MIN_VALUE;
            r.minInclude = true;
        }else if(minStr.equals("+inf") || minStr.equals("(+inf") || minStr.equals("[+inf")){
            r.min = Double.MAX_VALUE;
            r.minInclude = true;
        }else if(minStr.startsWith("(")){
            r.min = Double.parseDouble(minStr.substring(1));
            r.minInclude = false;
        }else if(minStr.startsWith("[")){
            r.min = Double.parseDouble(minStr.substring(1));
            r.minInclude = true;
        }else{
            r.min = Double.parseDouble(minStr);
            r.minInclude = true;
        }

        if(maxStr.equals("-inf") || maxStr.equals("(-inf") || maxStr.equals("[-inf")){
            r.max = Double.MIN_VALUE;
            r.maxInclude = true;
        }else if(maxStr.equals("+inf") || maxStr.equals("(+inf") || maxStr.equals("[+inf")){
            r.max = Double.MAX_VALUE;
            r.maxInclude = true;
        }else if(maxStr.startsWith("(")){
            r.max = Double.parseDouble(maxStr.substring(1));
            r.maxInclude = false;
        }else if(maxStr.startsWith("[")){
            r.max = Double.parseDouble(maxStr.substring(1));
            r.maxInclude = true;
        }else{
            r.max = Double.parseDouble(maxStr);
            r.maxInclude = true;
        }

        return r;
    }

}
